# 用户行为监控修复验证脚本
Write-Host "=== 用户行为监控修复验证 ===" -ForegroundColor Green

# 1. 检查前端userBehaviorService.js的修复
Write-Host "`n1. 检查前端trackAction方法的数据结构修复..." -ForegroundColor Yellow
$frontendFile = "d:/src/DotNet/Lzfy_Refund_Service/RefundAuditWeb/src/services/userBehaviorService.js"

if (Test-Path $frontendFile) {
    $content = Get-Content $frontendFile -Raw
    
    # 检查trackAction方法是否包含正确的数据结构
    if ($content -match "sessionId:\s*this\.currentSession\?\.\sessionId\s*\|\|\s*''") {
        Write-Host "✓ sessionId字段正确传递" -ForegroundColor Green
    } else {
        Write-Host "✗ sessionId字段传递有问题" -ForegroundColor Red
    }
    
    if ($content -match "taskDetailId:\s*this\.currentSession\?\.\taskDetailId\s*\|\|\s*null") {
        Write-Host "✓ taskDetailId字段正确传递" -ForegroundColor Green
    } else {
        Write-Host "✗ taskDetailId字段传递有问题" -ForegroundColor Red
    }
    
    if ($content -match "patientId:\s*this\.currentSession\?\.\patientId\s*\|\|\s*''") {
        Write-Host "✓ patientId字段正确传递" -ForegroundColor Green
    } else {
        Write-Host "✗ patientId字段传递有问题" -ForegroundColor Red
    }
    
    if ($content -match "actionData:\s*\{") {
        Write-Host "✓ actionData结构正确封装" -ForegroundColor Green
    } else {
        Write-Host "✗ actionData结构封装有问题" -ForegroundColor Red
    }
} else {
    Write-Host "✗ 前端服务文件不存在" -ForegroundColor Red
}

# 2. 检查后端UserBehaviorService.cs的关键方法
Write-Host "`n2. 检查后端UpdateOperationSessionAsync方法..." -ForegroundColor Yellow
$backendFile = "d:/src/DotNet/Lzfy_Refund_Service/Services/UserBehaviorService.cs"

if (Test-Path $backendFile) {
    $content = Get-Content $backendFile -Raw
    
    # 检查VIEW_PHONE时间记录
    if ($content -match "ActionTypes\.VIEW_PHONE.*PhoneViewTime\s*=\s*DateTime\.Now") {
        Write-Host "✓ VIEW_PHONE时间记录逻辑正确" -ForegroundColor Green
    } else {
        Write-Host "✗ VIEW_PHONE时间记录逻辑有问题" -ForegroundColor Red
    }
    
    # 检查INPUT_NOTES时间记录
    if ($content -match "ActionTypes\.INPUT_NOTES.*FirstInputTime.*LastInputTime") {
        Write-Host "✓ INPUT_NOTES时间记录逻辑正确" -ForegroundColor Green
    } else {
        Write-Host "✗ INPUT_NOTES时间记录逻辑有问题" -ForegroundColor Red
    }
    
    # 检查SAVE_CONTACT时间记录
    if ($content -match "ActionTypes\.SAVE_CONTACT.*SaveTime\s*=\s*DateTime\.Now") {
        Write-Host "✓ SAVE_CONTACT时间记录逻辑正确" -ForegroundColor Green
    } else {
        Write-Host "✗ SAVE_CONTACT时间记录逻辑有问题" -ForegroundColor Red
    }
    
    # 检查CalculateSessionDurations方法
    if ($content -match "CalculateSessionDurations\(session\)") {
        Write-Host "✓ 会话时长计算逻辑存在" -ForegroundColor Green
    } else {
        Write-Host "✗ 会话时长计算逻辑缺失" -ForegroundColor Red
    }
} else {
    Write-Host "✗ 后端服务文件不存在" -ForegroundColor Red
}

# 3. 检查前端RefundTaskManagement.vue的调用
Write-Host "`n3. 检查前端组件的行为跟踪调用..." -ForegroundColor Yellow
$vueFile = "d:/src/DotNet/Lzfy_Refund_Service/RefundAuditWeb/src/views/RefundTaskManagement.vue"

if (Test-Path $vueFile) {
    $content = Get-Content $vueFile -Raw
    
    # 检查startSession调用
    if ($content -match "userBehaviorService\.startSession\(") {
        Write-Host "✓ startSession调用存在" -ForegroundColor Green
    } else {
        Write-Host "✗ startSession调用缺失" -ForegroundColor Red
    }
    
    # 检查trackViewContact调用
    if ($content -match "userBehaviorService\.trackViewContact\(") {
        Write-Host "✓ trackViewContact调用存在" -ForegroundColor Green
    } else {
        Write-Host "✗ trackViewContact调用缺失" -ForegroundColor Red
    }
    
    # 检查trackSelectContactResult调用
    if ($content -match "userBehaviorService\.trackSelectContactResult\(") {
        Write-Host "✓ trackSelectContactResult调用存在" -ForegroundColor Green
    } else {
        Write-Host "✗ trackSelectContactResult调用缺失" -ForegroundColor Red
    }
    
    # 检查trackInputNotes调用
    if ($content -match "userBehaviorService\.trackInputNotes\(") {
        Write-Host "✓ trackInputNotes调用存在" -ForegroundColor Green
    } else {
        Write-Host "✗ trackInputNotes调用缺失" -ForegroundColor Red
    }
    
    # 检查trackSaveContact调用
    if ($content -match "userBehaviorService\.trackSaveContact\(") {
        Write-Host "✓ trackSaveContact调用存在" -ForegroundColor Green
    } else {
        Write-Host "✗ trackSaveContact调用缺失" -ForegroundColor Red
    }
    
    # 检查completeSession调用
    if ($content -match "userBehaviorService\.completeSession\(") {
        Write-Host "✓ completeSession调用存在" -ForegroundColor Green
    } else {
        Write-Host "✗ completeSession调用缺失" -ForegroundColor Red
    }
} else {
    Write-Host "✗ 前端组件文件不存在" -ForegroundColor Red
}

# 4. 检查后端Controller的接口
Write-Host "`n4. 检查后端Controller接口..." -ForegroundColor Yellow
$controllerFile = "d:/src/DotNet/Lzfy_Refund_Service/Controllers/UserBehaviorController.cs"

if (Test-Path $controllerFile) {
    $content = Get-Content $controllerFile -Raw
    
    # 检查TrackUserActionAsync接口
    if ($content -match "\[HttpPost\(\"track\"\)\].*TrackUserActionAsync") {
        Write-Host "✓ /api/UserBehavior/track接口存在" -ForegroundColor Green
    } else {
        Write-Host "✗ /api/UserBehavior/track接口缺失" -ForegroundColor Red
    }
    
    # 检查StartOperationSession接口
    if ($content -match "\[HttpPost\(\"session/start\"\)\].*StartOperationSession") {
        Write-Host "✓ /api/UserBehavior/session/start接口存在" -ForegroundColor Green
    } else {
        Write-Host "✗ /api/UserBehavior/session/start接口缺失" -ForegroundColor Red
    }
    
    # 检查CompleteOperationSession接口
    if ($content -match "\[HttpPost\(\"session/complete\"\)\].*CompleteOperationSession") {
        Write-Host "✓ /api/UserBehavior/session/complete接口存在" -ForegroundColor Green
    } else {
        Write-Host "✗ /api/UserBehavior/session/complete接口缺失" -ForegroundColor Red
    }
} else {
    Write-Host "✗ 后端控制器文件不存在" -ForegroundColor Red
}

Write-Host "`n=== 验证完成 ===" -ForegroundColor Green
Write-Host "请启动后端服务，然后在前端页面进行以下测试：" -ForegroundColor Cyan
Write-Host "1. 登录系统" -ForegroundColor White
Write-Host "2. 进入退费任务管理页面" -ForegroundColor White
Write-Host "3. 点击详情按钮查看任务详情" -ForegroundColor White
Write-Host "4. 点击电话号码按钮查看联系记录" -ForegroundColor White
Write-Host "5. 选择联系结果" -ForegroundColor White
Write-Host "6. 输入备注信息" -ForegroundColor White
Write-Host "7. 保存联系记录" -ForegroundColor White
Write-Host "8. 检查数据库中的用户行为日志记录是否包含正确的时间戳" -ForegroundColor White